﻿using System;
using System.Collections.Generic;
using System.Linq;
using ChamCongMain.Form;
using BLL;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Diagnostics;

namespace ChamCongMain.UserControl
{
    public partial class uclTinhLuong : DevExpress.XtraEditors.XtraUserControl
    {
        LuongBLL luongBLL = new LuongBLL();
        public uclTinhLuong()
        {
            InitializeComponent();
            LuongBLL lbll = new LuongBLL();
            RefeshG(lbll.GetAllLuong());
        }
        private void RefeshG(object formName)
        {
            grcLuong.DataSource = formName;
        }
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmTinhLuong frm = new frmTinhLuong();
            frm.LoadData = new Delegates.RefeshGrid(RefeshG);
            frm.ShowDialog();
        }

        private void btnSuaNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            Luong cn = (Luong)gvTinhLuong.GetRow(gvTinhLuong.FocusedRowHandle);
            if (cn == null)
            {
                XtraMessageBox.Show("Không có dữ liệu để sửa !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                frmLuong frm = new frmLuong(ChucNang.Sua, cn);
                frm.LoadData = new Delegates.RefeshGrid(RefeshG);
                frm.ShowDialog();
            }
            this.Cursor = Cursors.Default;
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            Luong cn = (Luong)gvTinhLuong.GetRow(gvTinhLuong.FocusedRowHandle);
            if (cn == null)
            {
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                try
                {
                    luongBLL.DeleteLuong(cn);
                    XtraMessageBox.Show("Xoá thành công!", "Xóa");
                    grcLuong.DataSource = luongBLL.GetAllLuong();
                }
                catch (Exception ee)
                {
                    if (ee.Message == "An error occurred while updating the entries. See the inner exception for details.")
                    {
                        XtraMessageBox.Show("không thể xóa công đoạn vì vi phạm khóa ngoại");
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    XtraMessageBox.Show("không thể xóa công đoạn vì vi phạm khóa ngoại");
                   // XtraMessageBox.Show(ee.ToString(), "Error");
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void btnXuatExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (gvTinhLuong.RowCount == 0)
            {
                XtraMessageBox.Show("Không có dữ liệu để xuất file !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = System.Windows.Forms.Cursors.Default;
                return;
            }
            DevExpress.XtraGrid.Views.Grid.GridView grid = grcLuong.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (grid != null)
            {
                SaveFileDialog saveExcel = new SaveFileDialog();
                saveExcel.Filter = "Excel Workbook|*.xlsx|Excel Macro-Enabled Workbook|*.xlsm|Excel 97-2003 Workbook|*.xls";// định giạng file lưu
                saveExcel.Title = "Save an Excel File";
                saveExcel.InitialDirectory = @"C:\";
                if (saveExcel.ShowDialog() == DialogResult.OK)
                {
                    this.Cursor = System.Windows.Forms.Cursors.Default;
                    grid.ExportToXlsx(saveExcel.FileName);
                    XtraMessageBox.Show("Export File thành công!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (XtraMessageBox.Show("Bạn có muốn xem file ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Process exportXlsx = new Process();
                        exportXlsx.StartInfo.FileName = "EXCEL.exe";
                        exportXlsx.StartInfo.Arguments = saveExcel.FileName;
                        exportXlsx.Start();
                    }
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void btnThem_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmLuong frm = new frmLuong(ChucNang.Them, null);
            frm.LoadData = new Delegates.RefeshGrid(RefeshG);
            frm.ShowDialog();
        }
    }
}
